define(["servers"], function (servers) {
    return  Backbone.Model.extend({
        defaults: function () {
            return {
                defListStatus:[{ id: "1", name: "有效",showIds:["0"] },{ id: "0", name: "无效",showIds:["1"] }],
                defListInteractions:[{id:"1",name:"Link跳转"},{id:"2",name:"APP下载"}],
                defListBuyType:[{id:1,name:"固定"},{id:2,name:"分成"},{id:3,name:"包断"}],
                defListChargeType:[{id:1,name:"按展示"},{id:2,name:"按点击"},{id:3,name:"按分成"}]
            };
        },
        getPageList:function(param){
            var self = this;
            servers.adSpace.pageList(param,function(res){
                self.set({dataPageList: res});
                self.trigger('bind:PageList');
            });
        },
        getInfo: function (id) {
            var self = this;
            if (id) {
                servers.adSpace.info(id, function (res) {
                    res.data=res.success?self.getMappingToWeb(res.data):{};
                    self.set({dataInfo: {isAdd:false,res:res}});
                    self.trigger('bind:Info');
                });
            } else {
                var dataDef={status: "0",adFormat:"1"};
                self.set({dataInfo:{isAdd:true,res:servers.getSendSuccess(dataDef)}});
                self.trigger('bind:Info');
            }
        },
        deleteInfo: function (id) {
            var self = this;
            servers.adSpace.delete(id,function (res) {
                self.set({dataDelete: res});
                self.trigger('save:Delete');
            });
        },
        addInfo: function (json) {
            var self = this;
            var data=this.getMappingToServer(json);
            servers.adSpace.add(data, function (res) {
                self.set({dataSave: {isAdd: true, req: json, res: res}});
                self.trigger('save:Info');
            });
        },
        updateInfo: function (json) {
            var self = this;
            var data=this.getMappingToServer(json);
            data.id=json.id;
            servers.adSpace.update(data, function (res) {
                self.set({dataSave: {isAdd: false, req: json, res: res}});
                self.trigger('save:Info');
            });
        },
        getMappingToWeb:function(json){
            var mapping = {
                "id":"id",
                "name":"name",
                "status":"status",
                "media": "pubSiteId",
                "adFormat": "formatId",
                "template": "templateId",
                "ratio": "ratioId",
                "description":"description",
                "interactions":"interactions",
                "imageType": "materialFormat",
                "buyType":"buyType",
                "chargeType":"chargeType",
                "chargePrice":"chargePrice"
            };
            var newJson = $.ExtCommon.getMappingJson(json, mapping);
            newJson.interactions=newJson.interactions=="3"?"1,2":newJson.interactions;
            return newJson
        },
        getMappingToServer:function(json){
            var mapping = {
                "id":"id",
                "name":"name",
                "status":"status",
                "pubSiteId": "media",
                "formatId": "adFormat",
                "templateId": "template",
                "ratioId": "ratio",
                "description":"description",
                "interactions":"interactions",
                "materialFormat": "imageType",
                "buyType":"buyType",
                "chargeType":"chargeType",
                "chargePrice":"chargePrice"
            };
            var newJson = $.ExtCommon.getMappingJson(json, mapping);
            if(newJson.formatId=="5"){
                delete newJson["ratioId"];
            }else{
                delete newJson["templateId"];
            }
            newJson.interactions=newJson.interactions.indexOf(",")>-1?"3":newJson.interactions;
            return newJson
        },
        updateStatus: function (param) {
            var self = this;
            servers.adSpace.updateStatus(param, function (res) {
                self.set({dataUpdStatus: {req:param,res:res}});
                self.trigger('update:Status');
            });
        }
    });
});